package com.xm.xmapp.base;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.FrameLayout;

import com.xm.xmapp.R;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.customview.LoadingDialog;

public abstract class BaseFragment extends Fragment implements BaseInterFace {

	protected View view;
	private FrameLayout fl_content_container;
	public Context context;
	public LoadingDialog promptDialog;

	public boolean isVisible;
	private boolean isPrepared = false;
	public String title;
	/**
	 * 是否需要懒加载
	 */
	public boolean isSuppertLazyLoad = true;

	public BaseFragment() {
	};

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		getData(savedInstanceState);
	};

	public void getData(Bundle savedInstanceState) {

	}

	Bundle savedState;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		// Restore State Here
		if (!restoreStateFromArguments()) {
			// First Time, Initialize something here
			onFirstTimeLaunched();
		}
	}

	protected void onFirstTimeLaunched() {

	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		// Save State Here
		saveStateToArguments();
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		saveStateToArguments();
	}

	private void saveStateToArguments() {
		if (getView() != null)
			savedState = saveState();
		if (savedState != null) {
			Bundle b = getArguments();
			if (b != null)
				b.putBundle("myStatus", savedState);
		}
	}

	private boolean restoreStateFromArguments() {
		Bundle b = getArguments();
		if (b != null) {
			savedState = b.getBundle("myStatus");
			if (savedState != null) {
				restoreState();
				return true;
			}
		}
		return false;
	}

	private void restoreState() {
		if (savedState != null) {

			onRestoreState(savedState);
		}
	}

	protected void onRestoreState(Bundle savedInstanceState) {

	}

	private Bundle saveState() {
		Bundle state = new Bundle();
		onSaveState(state);
		return state;
	}

	protected void onSaveState(Bundle outState) {

	}

	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {

		super.setUserVisibleHint(isVisibleToUser);

		if (isSuppertLazyLoad) {
			if (getUserVisibleHint()) {
				isVisible = true;
				lazyLoad();
				isPrepared = false;

			} else {
				isVisible = false;

			}
		}
	}

	public void lazyLoad() {
		if (isSuppertLazyLoad) {
			if (isPrepared && isVisible) {
				initData();
			}
		} else {
			initData();
		}

		return;
	}

	@SuppressLint("InflateParams")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		MyAppcation.getMyAppcation().addBaseInterFace(this);
		context = getActivity();
		promptDialog = new LoadingDialog(context);
		view = inflater.inflate(R.layout.fragment_base, null);
		fl_content_container = (FrameLayout) view
				.findViewById(R.id.fl_content_container);

		View content = LayoutInflater.from(getActivity()).inflate(
				setContentView(), null);
		fl_content_container.removeAllViews();
		fl_content_container.addView(content, new LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

		isPrepared = true;
		initView();
		lazyLoad();
		initEvent();

		view.setOnTouchListener(new OnTouchListener() {

			@SuppressLint("ClickableViewAccessibility")
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {

				return OnTouchListener(arg0, arg1);
			}
		});

		ViewTreeObserver vto2 = view.getViewTreeObserver();
		vto2.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
			@SuppressWarnings("deprecation")
			@Override
			public void onGlobalLayout() {

				view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
				ViewTreeObserver();
			}
		});

		return view;
	}

	protected boolean OnTouchListener(View arg0, MotionEvent arg1) {
		return false;

	}

	public void ViewTreeObserver() {

	}

	public View findViewById(int layoutResID) {

		return view.findViewById(layoutResID);

	}

	@Override
	public void onDestroy() {

		super.onDestroy();
		if (promptDialog != null) {
			promptDialog.dismiss();
		}
		MyAppcation.getMyAppcation().reMoveBaseInterFace(this);

	}



	public abstract int setContentView();

	public abstract void initView();

	public abstract void initData();

	public abstract void initEvent();

	@Override
	public void finishActivity() {
		MyAppcation.getMyAppcation().reMoveBaseInterFace(this);
	}

	@Override
	public Object dataChang(int flag, Object data) {
		return data;

	}

}
